package com.pig4cloud.mall.product.controller;

import com.pig4cloud.mall.product.dto.AttrParam;
import com.pig4cloud.mall.product.service.ProductAttributeService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商品属性参数表
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/attr")
@Api(value = "attr", tags = "商品属性参数表管理")
public class ProductAttributeController {

	private final ProductAttributeService attrService;


	@ApiOperation(value = "根据分类查询属性列表或参数列表", notes = "根据分类查询属性列表或参数列表")
	@GetMapping("/list/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_get')")
	public R getList(
			@PathVariable(value = "id") Long id,
			@RequestParam(value = "type") Integer type,
			@RequestParam(value = "current", defaultValue = "1") Integer current,
			@RequestParam(value = "size", defaultValue = "10") Integer size
	) {
		return R.ok(attrService.listAttr(id, type, current, size));
	}

	@ApiOperation(value = "通过分组id获取sku销售规则", notes = "通过分组id获取sku销售规则")
	@GetMapping("/sku/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_get')")
	public R skuRule(@PathVariable("id") Long cid) {
		return R.ok(attrService.getSkuRule(cid));
	}

	/**
	 * 通过id查询单个商品属性
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询单个商品属性", notes = "通过id查询单个商品属性")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_get')")
	public R getItem(@PathVariable(value = "id") Long id) {
		return R.ok(attrService.getItem(id));
	}

	/**
	 * 添加商品属性信息
	 *
	 * @param attrParam 前端请求商品属性
	 * @return R
	 */
	@ApiOperation(value = "添加商品属性信息", notes = "添加商品属性信息")
	@SysLog("添加商品属性信息")
	@PostMapping("/create")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_add')")
	public R create(@RequestBody AttrParam attrParam) {
		int count = attrService.create(attrParam);
		if (count > 0) {
			return R.ok();
		} else {
			return R.failed("添加失败！");
		}
	}

	/**
	 * 更新商品属性信息
	 *
	 * @param attrParam 更新商品属性信息
	 * @return R
	 */
	@ApiOperation(value = "更新商品属性信息", notes = "更新商品属性信息")
	@SysLog("更新商品属性信息")
	@PostMapping("/update/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_edit')")
	public R update(@PathVariable("id") Long id, @RequestBody AttrParam attrParam) {
		int count = attrService.update(id, attrParam);
		if (count > 0) {
			return R.ok();
		} else {
			return R.failed("更新失败！");
		}
	}

	/**
	 * 通过id删除商品属性参数
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除商品属性参数", notes = "通过id删除商品属性参数")
	@SysLog("通过id删除商品属性参数")
	@PostMapping("/delete/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_del')")
	public R delete(@PathVariable Long id) {
		int count = attrService.delete(id);
		if (count > 0) {
			return R.ok();
		} else {
			return R.failed("删除失败！");
		}
	}

}
